package com.helloags;

import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.server.IServerObjectAdmin;
import com.esri.arcgis.server.IServerObjectConfiguration2;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.server.esriServerIsolationLevel;
import com.esri.arcgis.server.esriStartupType;
import com.esri.arcgis.system.IPropertySet;
import com.esri.arcgis.system.ServerInitializer;

public class PublishService {
	static ServerConnection conn = null;
	static IServerObjectAdmin soa = null;
	public static void main(String[] args) throws UnknownHostException, IOException {
		ServerInitializer si = new ServerInitializer();
        si.initializeServer("*", "zhushijie", "zsj123456");
        
        conn = new ServerConnection(); 
		conn.connect("192.168.3.106", "*", "zhushijie", "zsj123456");
		
		soa = conn.getServerObjectAdmin();
		IServerObjectConfiguration2 conf = (IServerObjectConfiguration2)soa.createConfiguration();
		
		PublishService t = new PublishService();
		t.addMapServer(conf);
		t.addKMLServer(conf);
		t.addWMSServer(conf);
		t.addWFSServer(conf);
		t.addWCSServer(conf);
		
		soa.addConfiguration(conf);
		soa.startConfiguration("search5", "MapServer");
		System.out.println("Congratulaction!");
	}
	
	public boolean addMapServer(IServerObjectConfiguration2 conf){
		boolean flag = false;
		try {	
			conf.setName("search5");
			conf.setTypeName("MapServer");
			
			conf.getProperties().setProperty("FilePath", "E:\\zhu\\temp\\search.mxd");
			conf.getProperties().setProperty("MaxRecordCount", 1000);
			conf.getProperties().setProperty("MaxBufferCount", 100);
			conf.getProperties().setProperty("MaxImageWidth", 2048);
			conf.getProperties().setProperty("MaxImageHeight", 2048);
			conf.getProperties().setProperty("SupportedImageReturnTypes", "URL");
			conf.getProperties().setProperty("OutputDir", "c:\\arcgisserver\\arcgisoutput");
			conf.getProperties().setProperty("VirtualOutputDir", "http://192.168.3.106:8399/arcgis/server/arcgisoutput");
			
			//注意，这里的对象是String类型，不是boolean类型，不然会出错
			conf.getProperties().setProperty("IsCached", "false");
			conf.getProperties().setProperty("IgnoreCache", "false");
			conf.getProperties().setProperty("CacheOnDemand", "false");
			conf.getProperties().setProperty("ClientCachingAllowed", "true");
			
			conf.getProperties().setProperty("CacheDir", "c:\\arcgisserver\\arcgiscache\\search5");
			conf.getProperties().setProperty("SOMCacheDir", "c:\\arcgisserver\\arcgiscache");
			
//			conf.setDescription("search5");
			conf.setIsolationLevel(esriServerIsolationLevel.esriServerIsolationHigh);
			conf.setIsPooled(true);
			conf.setMinInstances(1);
			conf.setMaxInstances(5);
			
			conf.getRecycleProperties().setProperty("StartTime", "00:00");
			conf.getRecycleProperties().setProperty("Interval", "3600");
			
			conf.getInfo().setProperty("WebEnabled", "true");
			conf.getInfo().setProperty("WebCapabilities", "Map,Query,Data");
			conf.setStartupType(esriStartupType.esriSTAutomatic);
			
			conf.setUsageTimeout(120);
			conf.setWaitTimeout(120);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public boolean addKMLServer(IServerObjectConfiguration2 conf){
		boolean flag = false;
		try {
			conf.setExtensionEnabled("KmlServer", true);
			IPropertySet proKML = conf.getExtensionProperties("KmlServer");
			proKML.setProperty("FeatureLimit", "1000000");
			proKML.setProperty("ImageSize", "1024");
			proKML.setProperty("Dpi", "96");
			proKML.setProperty("UseNetworkLinkControlTag", "false");
			proKML.setProperty("MinRefreshPeriod", "30");
			proKML.setProperty("Composite", "true");
			proKML.setProperty("UseDefaultSnippets", "False");
			proKML.setProperty("EndPointURL", "http://light:8399/arcgis/services/search5/MapServer/KmlServer");
			
			conf.getExtensionInfo("KmlServer").setProperty("WebEnabled", "true");
			conf.getExtensionInfo("KmlServer").setProperty("WebCapabilities", "SingleImage,SeparateImages,Vectors");
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean addWMSServer(IServerObjectConfiguration2 conf){
		boolean flag = false;
		try {
			conf.setExtensionEnabled("WmsServer", true);
			IPropertySet proWMS = conf.getExtensionProperties("WmsServer");
			proWMS.setProperty("ContactPosition", "");
			proWMS.setProperty("OnlineResource", "http://light:8399/arcgis/services/search5/MapServer/WMSServer");
			proWMS.setProperty("ContactVoiceTelephone", "");
			proWMS.setProperty("ContactPerson", "");
			proWMS.setProperty("Fees", "");
			proWMS.setProperty("Name", "WMS");
			proWMS.setProperty("Country", "");
			proWMS.setProperty("Title", "search5");
			proWMS.setProperty("PostCode", "");
			proWMS.setProperty("ContactElectronicMailAddress", "");
			proWMS.setProperty("AccessConstraints", "");
			proWMS.setProperty("ContactOrganization", "");
			proWMS.setProperty("Keyword", "");
			proWMS.setProperty("Abstract", "");
			proWMS.setProperty("Address", "");
			proWMS.setProperty("ContactFacsimileTelephone", "");
			proWMS.setProperty("AddressType", "");
			proWMS.setProperty("StateOrProvince", "");
			proWMS.setProperty("City", "");
			proWMS.setProperty("PathToCustomSLDFile", "");
			proWMS.setProperty("InheritLayerNames", "false");
			conf.getExtensionInfo("WmsServer").setProperty("WebEnabled", "true");
			conf.getExtensionInfo("WmsServer").setProperty("WebCapabilities", "");
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean addWFSServer(IServerObjectConfiguration2 conf){
		boolean flag = false;
		try {
			conf.setExtensionEnabled("WfsServer", true);
			IPropertySet proWFS = conf.getExtensionProperties("WfsServer");
			proWFS.setProperty("OnlineResource", "");
			proWFS.setProperty("Role", "");
			proWFS.setProperty("Fees", "");
			proWFS.setProperty("Name", "");
			proWFS.setProperty("Country", "");
			proWFS.setProperty("Title", "");
			proWFS.setProperty("DeliveryPoint", "");
			proWFS.setProperty("ProviderSite", "");
			proWFS.setProperty("AccessConstraints", "");
			proWFS.setProperty("Keyword", "");
			proWFS.setProperty("ContactInstructions", "");
			proWFS.setProperty("PositionName", "");
			proWFS.setProperty("Phone", "");
			proWFS.setProperty("Facsimile", "");
			proWFS.setProperty("AdministrativeArea", "");
			proWFS.setProperty("Abstract", "");
			proWFS.setProperty("HoursOfService", "");
			proWFS.setProperty("ProviderName", "");
			proWFS.setProperty("PostalCode", "");
			proWFS.setProperty("ServiceType", "");
			proWFS.setProperty("IndividualName", "");
			proWFS.setProperty("City", "");
			proWFS.setProperty("ServiceTypeVersion", "");
			proWFS.setProperty("ElectronicMailAddress", "");
			proWFS.setProperty("AppSchemaPrefix", "search5");
			proWFS.setProperty("EnableTransactions", "false");
			proWFS.setProperty("AppSchemaURI", "http://light:8399/arcgis/services/search5/MapServer/WFSServer");
			conf.getExtensionInfo("WfsServer").setProperty("WebEnabled", "true");
			conf.getExtensionInfo("WfsServer").setProperty("WebCapabilities", "");
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean addWCSServer(IServerObjectConfiguration2 conf){
		boolean flag = false;
		try {
			conf.setExtensionEnabled("WcsServer", true);
			IPropertySet proWCS = conf.getExtensionProperties("WcsServer");
			proWCS.setProperty("Province", "");
			proWCS.setProperty("OnlineResource", "http://light:8399/arcgis/services/search5/MapServer/WCSServer");
			proWCS.setProperty("MaxImageWidth", "");
			proWCS.setProperty("Fax", "");
			proWCS.setProperty("Role", "");
			proWCS.setProperty("Fees", "");
			proWCS.setProperty("Name", "search5");
			proWCS.setProperty("Country", "");
			proWCS.setProperty("Title", "search5");
			proWCS.setProperty("ResponsiblePerson", "");
			proWCS.setProperty("AccessConstraints", "");
			proWCS.setProperty("MaxImageHeight", "");
			proWCS.setProperty("Keyword", "");
			proWCS.setProperty("ContactInstructions", "");
			proWCS.setProperty("Phone", "");
			proWCS.setProperty("ResponsiblePosition", "");
			proWCS.setProperty("ProviderWebsite", "");
			proWCS.setProperty("Zipcode", "");
			proWCS.setProperty("Abstract", "");
			proWCS.setProperty("Address", "");
			proWCS.setProperty("ProviderName", "");
			proWCS.setProperty("Email", "");
			proWCS.setProperty("ServiceHour", "");
			proWCS.setProperty("City", "");
			conf.getExtensionInfo("WcsServer").setProperty("WebEnabled", "true");
			conf.getExtensionInfo("WcsServer").setProperty("WebCapabilities", "");
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}
